$(function(){
	$('.end,.placenum').bind('click',$(this).showlist);
	$(window).bind('click',function(){
		$('.end,.placenum').removeClass('act').find('.db').hide();
	});
	var fun = $.showichimg();
	var index = parseInt(window.location.hash.split('#')[1]);
	if(!index){index = 0;}
	$('.detailinfo .content').eq(index).show();
	$('.menulist .menuone').removeClass('act').eq(index).addClass('act');
	window.act = 0;
	window.imgsrclist = [];
	$('.imgone').eq(0).addClass('act');
	$('.imgone').each(function(i){
		imgsrclist[i] = $(this).find('img').attr('bigstsrc');
	})
	$('.magnifier').unbind().bind('click',function(){
		imgview(imgsrclist,act);
	});
	$('.bshare-custom a').click(function(){
		alert($(this).index());
	})
	$('.sharebox .btn').click(function(){		
		$('.bshare-custom a').eq($(this).index()).click();
	});
	// 工艺流程begin
	var imgh = $('.contbox .oneimgt').eq(0).find('img').height()+20,ph = $('.contbox .oneimgt').eq(0).find('p').height();
	$('.contbox').setwidth().find('.oneimgt').each(function(){
		$(this).hover(function(){
			if($(this).find('img').index()==0)
				$(this).find('img').stop().animate({'margin-top':-imgh+'px'},300);
			$(this).find('p').stop().animate({'height':'+='+imgh+'px'},300);
		},function(){
			if($(this).find('img').index()==0)
				$(this).find('img').stop().animate({'margin-top':'0'},300);
			$(this).find('p').stop().animate({'height':ph+'px'},300);
		});
	});
	//工艺流程end
	//最近浏览begin
	var over = true;
	$('.newread .one').each(function(){
		$(this).hover(function(){
			over = false;
			$.each($('.newread .one').not(this),function(){
				if($(this).find('.alphac').length){
					var a = $(this).find('.alphac');
				}else{
					var a = $(this).append('<span class="alphac"></span>').find('.alphac')
				}
				a.stop().fadeIn(200);
			})
			if($(this).find('.alphac').length)
				$(this).find('.alphac').stop().fadeOut(10);
		},function(){
			over = true;
			$('.newread .one').each(function(){
				if($(this).find('.alphac').length){
					var self = $(this);
					setTimeout(function(){
						if(over)
						self.find('.alphac').fadeOut(100);
					},200);
				}
			})
		})
	})
	//最近浏览end
});
// $(window).scroll(function(){
// 	var winscroll = function(){
// 		var st = $(window).scrollTop();
// 		if(st>$('.detailinfo').offset().top){
// 			$('.detailmenu').css({'position':'fixed','left':'50%','top':'0','marginLeft':'-489px',"width":"750px",'zIndex':'4'}).find('.btnbox').show();
// 		}else{
// 			$('.detailmenu').attr('style','').find('.btnbox').hide();
// 		}
// 	};
// 	winscroll();
// });
$.fn.extend({showlist:function(event){
	event.stopPropagation();
	$(this).each(function(){
		var self = this;
		$(this).addClass('act').find('.db').show().find('.dbone').each(function(){
			$(this).bind('click',function(event){
				event.stopPropagation();
				$(self).find('span').html($(this).html());
				//$('#carriageprice').html("快递："+$(this).attr('value')+"元");
				$(self).removeClass('act').find('.db').hide();
			});
		});
	})
},momove:function(){
	var all = $(this);
	$(this).each(function(){
		var self = $(this),mbox = $(this).find('.movebox');
		if(mbox.width()>self.width())
		self.mousemove(function(event){
			var x = event.pageX - $(this).offset().left,w = self.width(),left = parseInt((x-150)/(w-300)*(mbox.width()-w));
			if(x>150&&x<850){
				mbox.css('left',-left+'px');
				return ;
			}
			if (x<=150) {
				mbox.css('left','0');
				return;
			}
			if(x>=850)
				mbox.css('left',-(mbox.width()-w)+'px');
		})
	});
	return this;
},
setwidth:function(){
	var one = $(this).find('.oneimgt'),other = $(this).find('.onetext'),w = one.length*(one.eq(0).width()+parseInt(one.eq(0).css('margin-left')))+other.width()+30;
	$(this).find('.movebox').css('width',w+'px');
	$(this).momove();
	return this;
}
}); 
(function($){
	$.extend({
		showichimg:function(options){
			var defaults = {
				bigimg:$('.disspace'),
				imgwrap : $('.ichimgwrap'),
				imgitemwrap : $('.imglist ul'),
				imgitem : $('.imglist .imgone img'),
				act:0
			},
			settings = $.extend({},defaults,options);
			settings.imgitem.each(function(){
				$(this).click(function(){
					var self = $(this);
					act = $(this).parent().index();
					$('.imgone').removeClass('act').eq(act).addClass('act');
					$('.magnifier').unbind().bind('click',function(){
						imgview(imgsrclist,act);
					});
					var img = new Image();
					img.onload = function(){
						img.onload = null;
						settings.bigimg.attr('src',self.attr('bimg'));
					}
					img.src = $(this).attr('bimg');
					settings.bigimg.attr('src','page/topic/paimai/images/loadimg.png').attr('bigstsrc',$(this).attr('bigstsrc'));
				});
			});
		},listclick:function(i){
			$('html,body').animate({'scrollTop':$('.detailinfo').offset().top+"px"},100);
			$('.menulist .menuone').removeClass('act').eq(i).addClass('act');
			$('.detailinfo').find('.content').hide().eq(i).show();
		},newreadGoods:function(id){
			var newread,nothis,nohave = true,leng = 8;
			if($.cookie&&$.cookie('newread')){
				newread = $.cookie('newread');
				$.each($.cookie('newread').split(','),function(i,o){
					if(o==id){
						newread = _.without($.cookie('newread').split(','),id).toString();
						nohave = false;
						return false;
					}
				})
				if(newread.split(',').length>leng){
					var over = newread.split(',').length - leng;
					if(nohave){
						nothis = _.initial($.cookie('newread').split(','),over).toString();
					}
				}else{
					nothis = newread;
				}
			}
			if(newread){
				$.cookie('newread',id+','+nothis,{expires:365,path:'/'});
			}else{
				$.cookie('newread',id,{expires:365,path:'/'});
			}
			return nothis;
		}
	});
})(jQuery);
var scord = function(element){
	var scord = this;
	this.act =1;
	this.timer="";
	this.W=parseInt(element.box.css("width"));
	var oneW = parseInt(element.SlideAll.eq(0).css("width"));
	this.online = parseInt(this.W/oneW);
	if(element.SlideAll.length/this.online-parseInt(element.SlideAll.length/this.online)>0)
		this.leng=parseInt(element.SlideAll.length/this.online)+1;
	else
		this.leng = element.SlideAll.length/this.online;
	this.element = element;
	if(this.element.SlideAll.length>0){
		this.onthW = this.element.SlideAll.eq(0).outerWidth()+parseInt(this.element.SlideAll.eq(0).css('marginLeft'))+parseInt(this.element.SlideAll.eq(0).css('marginRight'));
		var this_ = this;
		element.box.css({'height':'136px','width':this_.onthW*this_.online-parseInt(this.element.SlideAll.eq(0).css('marginLeft'))-parseInt(this.element.SlideAll.eq(0).css('marginRight'))+'px'});
		this.W = this_.onthW*this_.online;
		// this.element.leftobj.css({'marginLeft':-(this.W-parseInt(this.element.SlideAll.eq(0).css('marginLeft'))-parseInt(this.element.SlideAll.eq(0).css('marginRight')))/2-60+'px'});
		// this.element.rightobj.css({'marginLeft':(this.W-parseInt(this.element.SlideAll.eq(0).css('marginLeft'))-parseInt(this.element.SlideAll.eq(0).css('marginRight')))/2+20+'px'});
	}
	
	if(this.leng>1){
		this.init();
		this.center();
		this.element.leftobj.show();
		this.element.rightobj.show();
	}else{
		this.element.leftobj.remove();
		this.element.rightobj.remove();
	}
};
scord.prototype = 
	{
		star:function(){
			this.left();
		},
		left:function(){
			var this_ = this;
			if(this_.act == this_.leng+1){
				this_.putong();
				setTimeout(function(){
					this_.act = 1;
					this_.go();
				},500)
			}else{
				this_.putong();
			}
		},
		right:function(){
			var this_ = this;
			if(this.act == 0){
				this_.putong();
				setTimeout(function(){
					this_.act = this_.leng;
					this_.go();
				},500)
			}else{
				this_.putong();
			}
		},
		putong:function(){
			this.ret();
			this.goAni();
		},
		go:function(){
			var this_ = this;
			this.element.slideS.css({"left":-this_.W*this_.act+"px"});
		},
		goAni:function(){
			var this_ = this;
			this.element.slideS.stop(true,true);
			this.element.slideS.animate({left:-this_.W*this_.act+"px"},300);
			this.element.leftobj.unbind();
			this.element.rightobj.unbind();
			setTimeout(function(){
				this_.element.leftobj.click(function(){
					this_.act--;
					this_.right();
				})
				this_.element.rightobj.click(function(){
					this_.act++;
					this_.left();
				})
			},300)
		},
	
		ret:function(){
			var delact = this.act;
			for(var i=0;i<this.leng;i++)
				this.element.dian.children("i").eq(i).removeClass("act");
			if(this.act==this.leng+1)
				delact = 1;
			else if(this.act == 0)
				delact = this.leng;
			this.element.dian.children("i").eq(delact-1).addClass("act");
		}
	};

scord.prototype.init =function(){
	var this_ = this;
	var delNode = $("<div></div>");
	delNode.addClass(this_.element.dian);
	this_.element.box.append(delNode);
	this_.element.dian = $("."+this_.element.dian);
	var str = "";
	for(var i=0;i<this.leng;i++){
		str += "<i></i>";
	}
	for(var i=0;i<this.element.SlideAll.length;i++)
		this.element.SlideAll.eq(i).attr('list',i);
	this_.element.dian.html(str);
	var clone1 =[],clone2 = [];
	for(var i=0;i<this.online;i++)
		clone1.push(this_.element.SlideAll.eq(i).clone(true));
	$(clone1).find("img").attr("src",$(clone1).find("img").attr("lo"));
	var mowei = this.element.SlideAll.length%this.online!=0?this.element.SlideAll.length%this.online:this.online;
	var marRleng = this.online-mowei;
	this.element.SlideAll.eq(-1).css('margin-right',marRleng*this_.onthW+parseInt(this.element.SlideAll.eq(-1).css('marginRight'))+"px");
	for(var i=0;i<mowei;i++)
		clone2.push(this_.element.SlideAll.eq(this.element.SlideAll.length-(i+1)).clone(true));
	
	for(var i=0;i<clone1.length;i++)
		this_.element.slideS.append(clone1[i]);
	for(var i=0;i<clone2.length;i++)
		this_.element.slideS.prepend(clone2[i]);
	$(clone2).find("img").attr("src",$(clone2).find("img").attr("lo"));
	this_.element.slideS.css({width:this_.W*(this_.leng+2)*100+"%",position:"absolute"});
	this_.element.dian.children("i").eq(0).addClass("act" );
	this.go();
	this.automove();
	this.hoved();
	this.clickFun();

};
scord.prototype.automove=function(){
	var this_ = this;
	this.timer = setInterval(function(){
		this_.act++;
		this_.star();
	},5000)
};
scord.prototype.hoved=function(){
	var this_ = this;
	this.element.box.hover(function(){
		clearInterval(this_.timer);
	},function(){
		this_.automove();
	})
};
scord.prototype.clickFun=function(){
	var this_ = this;
	this.element.leftobj.unbind();
	this.element.rightobj.unbind();
	this.element.leftobj.click(function(){
		this_.act--;
		this_.right();
	})
	this.element.rightobj.click(function(){
		this_.act++;
		this_.left();
	})
	this.element.dian.children("i").each(function(i){
		$(this).click(function(){
			this_.act = $(this).index()+1;
			this_.left();
		})
	})
};
scord.prototype.center = function(){
	var this_ = this;
	this.element.dian.css("margin-left",-this_.leng*this_.element.dian.children("i").width()/2+"px");
};


